import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:mobile_app.core/core/presentation/helpers/screen_util_mixin.dart';
import 'package:mobile_app.core/core/presentation/theme/theme.dart';
import 'package:mobile_app.core/core/presentation/widgets/tiles/default_size_tile.dart';
import 'package:mobile_app.core/core/presentation/widgets/tiles/text_tile_premium_badge_gap.dart';
import 'package:mobile_app.core/core/presentation/widgets/tiles/tile_premium_badge.dart';

class BaseTextTile extends StatelessWidget with ScreenUtilMixin {
  const BaseTextTile({
    this.title,
    this.titleTextStyle,
    this.titleText,
    this.subtitle,
    this.leading,
    this.trailing,
    this.showPremiumBadge = false,
    super.key,
  }) : assert(title == null && titleText != null || title != null && titleText == null);

  final String? title;
  final TextStyle? titleTextStyle;
  final Text? titleText;
  final String? subtitle;
  final Widget? leading;
  final Widget? trailing;
  final bool showPremiumBadge;

  Widget _buildLeading() {
    if (leading == null) {
      return const SizedBox(
        width: 16,
      );
    } else {
      return leading!;
    }
  }

  Widget _buildTrailing() {
    if (trailing == null) {
      return const SizedBox(
        width: 16,
      );
    } else {
      return trailing!;
    }
  }

  Widget _buildTitleTextWithBadge({
    required Text effectiveTitleText,
  }) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Flexible(
          child: effectiveTitleText,
        ),
        if (showPremiumBadge) ...[
          const TextTilePremiumBadgeGap(),
          const TilePremiumBadge(),
        ],
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    var effectiveTitleText = titleText;

    effectiveTitleText ??= Text(
      title!,
      style: titleTextStyle ?? CoreTheme.of(context).textTheme.tileTitle,
      textWidthBasis: TextWidthBasis.longestLine,
    );

    late Widget body;
    if (subtitle == null) {
      body = Align(
        alignment: Alignment.centerLeft,
        child: Padding(
          padding: const EdgeInsets.symmetric(
            vertical: 10,
          ),
          child: _buildTitleTextWithBadge(
            effectiveTitleText: effectiveTitleText,
          ),
        ),
      );
    } else {
      body = Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            constraints: const BoxConstraints(
              minHeight: 34,
            ),
            child: Align(
              alignment: Alignment.centerLeft,
              child: Padding(
                padding: const EdgeInsets.only(
                  top: 10,
                  bottom: 5,
                ),
                child: _buildTitleTextWithBadge(
                  effectiveTitleText: effectiveTitleText,
                ),
              ),
            ),
          ),
          Text(
            subtitle!,
            style: CoreTheme.of(context).textTheme.tileSubtitle,
          ),
          const SizedBox(
            height: 8,
          ),
        ],
      );
    }

    return DefaultSizeTile(
      child: Row(
        children: [
          _buildLeading(),
          Expanded(
            child: SizedBox(
              width: double.infinity,
              child: body,
            ),
          ),
          _buildTrailing(),
        ],
      ),
    );
  }
}
